<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/
// no direct access
defined('_JEXEC') or die('Restricted access');
    class JHTMLInput {
        
        function formValidation($com){
            $document =& JFactory::getDocument();
            $document->addStyleSheet(JURI::base().'components/'.$com.'/assets/css/formcheck.css'); 
            $script = "<script type='text/javascript' src='".JURI::base()."components/$com/assets/js/formcheck.js'>
            </script>
            <script type='text/javascript'>
                	var myCheck ;            
    			window.addEvent('domready', function() {
    			myCheck = new FormCheck('adminForm', {
    				tipsClass : 'fc-tbx',
    				display : {
    					showErrors : 1,					//0 : onSubmit, 1 : onSubmit & onBlur
            			errorsLocation : 1,				//1 : tips, 2 : before, 3 : after
            			indicateErrors : 1,				//0 : none, 1 : one, 2 : all
            			keepFocusOnError : 1,			//0 : normal behaviour, 1 : field keep the focus as it remain errors
            			addClassErrorToField : 1,		//0 : normal behaviour, 1: a class (fieldErrorClass) is added to the field, to highlight it for example
            			fixPngForIe : 1,				//0 : do nothing, 1 : fix png
            			replaceTipsEffect : 1,			//0 : no effect on tips replace when we resize the window, 1: tween transition on window resize
            			flashTips : 0,					//0 : desactivated, 1 : the tips is highlited if errors remains on submit.
            			closeTipsButton : 1,			//0 : button is hidden, 1 : button is visible
            			tipsPosition : 'right',			//to place the tips on the left or on the right of the field
            			tipsOffsetX : -45,				//Left position of the tips box (margin-left)
            			tipsOffsetY : 0,				//Top position of the tips box (margin-bottom)
            			listErrorsAtTop : false,		//list all errors at the top of the form
            			scrollToFirst : true,			//Smooth scroll the page to first error
            			fadeDuration : 400				//Transition duration
                    				},
    				alerts : {
    					required : 'This field is ablolutely required! Please enter a value'
    				}
    			})
        		});
                
                function submitform(pressbutton){
                    if (pressbutton) {
                		  document.adminForm.task.value=pressbutton;
                   	}
                    
                    if(pressbutton == 'cancel'){
                        document.adminForm.submit();
                    } else {        
                        myCheck.form.fireEvent('submit'); 
                        
                    	if (typeof document.adminForm.onsubmit == 'function') {
                    		document.adminForm.onsubmit();
                    	}
                        if(myCheck.form.isValid){
                            document.adminForm.submit();
                        }
                    }                            
                }
        	</script>";
            $document->addCustomTag($script);
        }
        
    function option($value, $text, $att = '', $disable = false) {
        $obj = new stdClass;
        $obj->value = $value;
        $obj->text = $text;
        $obj->disable = $disable;
        $obj->att = $att;

        return $obj;
    }
    
    function radioList($arr, $name, $label, $default = '', $option = array()) {
        $class          = $option['class'] !=''    ?  $option['class'] : '';
        $requiredOption = $option['requiredOption'] != '' ? $option['requiredOption'] : 'validate["required"]'; 
        $labelClass     = $option['labelClass'] ? $option['labelClass'] : '';       
        $disable        = $option['disable'] ? 'disable="disable"' : '';
        $defaults       = $default != '' ? explode('|', $default) : array();
         
        $radio  = JHTML::_('input.label',$label, $name, "class='$labelClass'");        
        $radio .= '<div class="radiolist">';
        $i = 0;
        if (count($arr)) {
            foreach ($arr as $opt) {                
                $class .= $option['required'] && $i == 0 ? $requiredOption.''  : '';
                $class  = ($class != '') ? "class='$class'": '';
                
                $radio .= '<div class="radio">';
                $radio .= "<input type='radio' name='$name' id='" . $name.$i . "' value='" . $opt->value . "' $class $checked $disable $opt->att />";
                $radio .= JHTML::_('input.label',$opt->text, $name.$i, "");
                $radio .= '</div>'; $i++;
            }
        }
        $radio .= '</div>';
        
        return $radio;
    }

    function textbox($label, $name, $default = '', $att = '', $option = array()) {        
        $class          = isset($option['class'] )   ? $option['class'] : '';
        $requiredOption = isset($option['requiredOption']) != '' ? $option['requiredOption'] : 'validate["required"]';       
        $class         .= isset($option['required']) ? $requiredOption.' text-input'  : 'text-input';   
             
        $labelClass = isset($option['labelClass']) ? $option['labelClass'] : '';       
        $disable    = isset($option['disable']) ? 'disable="disable"' : '';
        $type       = isset($option['hidden']) ? 'hidden':'text';
        
        $textbox  = $label != ''? JHTML::_('input.label',$label, $name, "class='$labelClass'") : '';   
        $textbox .= "<input id='$name' type='$type' name='$name' value='$default' class='input $class' $disable $att/>";

        return $textbox;

    }
    
    function textarea($label, $name, $default = '', $att = '', $option = array()) {
        $cols       = isset($option['cols'])    ? $option['cols'] : 26;
        $rows       = isset($option['rows'])    ? $option['rows'] : 3;        
        $class      = isset($option['class'])    ? $option['class'] : '';
        $requiredOption = isset($option['requiredOption']) != '' ? $option['requiredOption'] : 'validate["required"]';       
        $class     .= isset($option['required']) ? $requiredOption.' text-input'  : 'text-input'; 
        $labelClass = isset($option['labelClass']) ? $option['labelClass'] : '';       
        $disable    = isset($option['disable']) ? 'disable="disable"' : '';
        $type       = isset($option['hidden']) ? 'hidden':'text';
        
        $textbox  = $label != ''? JHTML::_('input.label',$label, $name, "class='$labelClass'") : '';   
        $textbox .= "<textarea id='$name' type='$type' name='$name' cols='$cols' rows='$rows' class='input $class' $disable $att>$default</textarea>";

        return $textbox;

    }
    
    function label($label, $name, $att = '') {
        $label  = "<label for='$name' $att>" . JText::_($label) . "</label>";

        return $label;

    }
    
    /**
	 * Generates just the option tags for an HTML select list
	 *
	 * @param	array	An array of objects
	 * @param	string	The name of the object variable for the option value
	 * @param	string	The name of the object variable for the option text
	 * @param	mixed	The key that is selected (accepts an array or a string)
	 * @returns	string	HTML for the select list
	 */
	function options( $arr, $key = 'value', $text = 'text', $selected = null, $translate = false )
	{
		$html = '';

		foreach ($arr as $i => $option)
		{
			$element =& $arr[$i]; // since current doesn't return a reference, need to do this

			$isArray = is_array( $element );
			$extra	 = '';
			if ($isArray)
			{
				$k 		= $element[$key];
				$t	 	= $element[$text];
				$id 	= ( isset( $element['id'] ) ? $element['id'] : null );
				if(isset($element['disable']) && $element['disable']) {
					$extra .= ' disabled="disabled"';
				}
			}
			else
			{
				$k 		= $element->$key;
				$t	 	= $element->$text;
				$id 	= ( isset( $element->id ) ? $element->id : null );
				if(isset( $element->disable ) && $element->disable) {
					$extra .= ' disabled="disabled"';
				}
                if(isset( $element->att ) && $element->att) {
					$extra .= $element->att;
				}
			}

			// This is real dirty, open to suggestions,
			// barring doing a propper object to handle it
			if ($k === '<OPTGROUP>') {
				$html .= '<optgroup label="' . $t . '">';
			} else if ($k === '</OPTGROUP>') {
				$html .= '</optgroup>';
			}
			else
			{
				//if no string after hypen - take hypen out
				$splitText = explode( ' - ', $t, 2 );
				$t = $splitText[0];
				if(isset($splitText[1])){ $t .= ' - '. $splitText[1]; }

				//$extra = '';
				//$extra .= $id ? ' id="' . $arr[$i]->id . '"' : '';
				if (is_array( $selected ))
				{
					foreach ($selected as $val)
					{
						$k2 = is_object( $val ) ? $val->$key : $val;
						if ($k == $k2)
						{
							$extra .= ' selected="selected"';
							break;
						}
					}
				} else {
					$extra .= ( (string)$k == (string)$selected  ? ' selected="selected"' : '' );
				}

				//if flag translate text
				if ($translate) {
					$t = JText::_( $t );
				}

				// ensure ampersands are encoded
				$k = JFilterOutput::ampReplace($k);
				$t = JFilterOutput::ampReplace($t);

				$html .= '<option value="'. $k .'" '. $extra .'>' . $t . '</option>';
			}
		}

		return $html;
	}

	/**
	 * Generates an HTML select list
	 *
	 * @param	array	An array of objects
	 * @param	string	The value of the HTML name attribute
	 * @param	string	Additional HTML attributes for the <select> tag
	 * @param	string	The name of the object variable for the option value
	 * @param	string	The name of the object variable for the option text
	 * @param	mixed	The key that is selected (accepts an array or a string)
	 * @returns	string	HTML for the select list
	 */
	function genericlist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false )
	{
		if ( is_array( $arr ) ) {
			reset( $arr );
		}

		if (is_array($attribs)) {
			$attribs = JArrayHelper::toString($attribs);
		 }

		$id = $name;

		if ( $idtag ) {
			$id = $idtag;
		}

		$id		= str_replace('[','',$id);
		$id		= str_replace(']','',$id);

		$html	= '<select name="'. $name .'" id="'. $id .'" '. $attribs .'>';
		$html	.= JHTMLInput::Options( $arr, $key, $text, $selected, $translate );
		$html	.= '</select>';

		return $html;
	}

      
}